<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Home\Controller;
use Think\Controller;
/**
 * 用户控制器
 * @author jry <598821125@qq.com>
 */
class UserController extends HomeController{
    /**
     * 默认方法
     * @author jry <598821125@qq.com>
     */
    public function index(){
        $this->assign('meta_title', "首页");
        $this->display();
    }

    /**
     * 用户注册
     * @author jry <598821125@qq.com>
     */
    public function register(){
        if(IS_POST){
            $username = $_POST['username'];
            $password = $_POST['password'];
            $user = D('Admin/User');
            $data = $user->create();
            if($data){
                $id = $user->add();
                if($id){
                    $uid = D('Admin/User')->login($username, $password);
                    $this->success('注册成功', U('Index/index'));
                }else{
                    $this->error('注册失败');
                }
            }else{
                $this->error($user->getError());
            }
        }else{
            $this->meta_title = '用户注册';
            $this->display();
        }
    }

    /**
     * 登陆
     * @author jry <598821125@qq.com>
     */
    public function login(){
        if(IS_POST){
            $username = I('username');
            $password = I('password');
            $uid = D('Admin/User')->login($username, $password);
            if(0 < $uid){
                $this->success('登录成功！', U('Index/index'));
            }else{
                $this->error('登录失败！');
            }
        }else{
            $this->meta_title = '用户登录';
            $this->display();
        }
    }

    /**
     * 注销
     * @author jry <598821125@qq.com>
     */
    public function logout(){
        session('user_auth', null);
        session('user_auth_sign', null);
        $this->success('退出成功！', U('User/login'));
    }
}
